﻿using BI.CF.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BI.CF.BLL;
using System.IO;

namespace BI.CF.Company.Controllers
{
    public class AuthonymController : Controller
    {
        //
        // GET: /Authonym/

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 实名认证页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Authonym_ADD()
        {
            if (Session["Umodel"] == null)
            {
                return Content("<script>location.href='/MyAccount/View_UserLogin'</script>");
            }
            else
            {
                return View();
            }
        }


        static string urlPath = string.Empty;
        public AuthonymController() 
        {
            var applicationPath = VirtualPathUtility.ToAbsolute("~") == "/" ? "" : VirtualPathUtility.ToAbsolute("~");
            urlPath = applicationPath + "/Upload";
        
        }
        /// <summary>
        /// 上传方法
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="lastModifiedDate"></param>
        /// <param name="size"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public ActionResult UpLoadProcess(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
        {
            string filePathName = string.Empty;

            string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Upload");
            if (Request.Files.Count == 0)
            {
                return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
            }
           string ex = Path.GetExtension(file.FileName);
            filePathName = Guid.NewGuid().ToString("N") + ex;
            if (!System.IO.Directory.Exists(localPath))
            {
                System.IO.Directory.CreateDirectory(localPath);
            }
            file.SaveAs(Path.Combine(localPath, filePathName));

            var url = "";
            url = urlPath + "/" + filePathName;
            Autonym_BLL Aubll = new Autonym_BLL();
            AutonymModel m = new AutonymModel();
            m.CerfiticationPapers = url;
            m.CerfiticationTime = DateTime.Now;
            m.IdCard = Request["IdCard"].ToString();
            m.RealNameState = false;
            m.UserId = int.Parse(Request["UserID"].ToString());
            if (Aubll.Autonym_ADD(m) != true)
            {
                return Content("<script>alert('失败')</script>");

            }
            else
            {
                return Json(new
                {
                    jsonrpc = "2.0",
                    id = id,
                    filePath = urlPath + "/" + filePathName
                });
            }
            
        }

        /// <summary>
        /// 查询用户是否实名
        /// </summary>
        /// <returns></returns>
        public int Authonym_State()
        {
            Autonym_BLL a = new Autonym_BLL();
            int id = int.Parse(Request["id"].ToString());
            AutonymModel m = a.Get_Authonym_Only(id);
            if (m.RealNameState == false)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
       

       
       
           
    }
}

